package de.klingbeil.hhcalc.ui.application;

import org.springframework.stereotype.Component;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

import de.klingbeil.hhcalc.backend.domain.HhcalcItem;

@Component
public class MainView extends VerticalLayout {

	private static final long serialVersionUID = 1L;

	public MainView() {
		HhcalcItem item = new HhcalcItem();
		item.setFirstName("TOm");
		
		BeanItem<HhcalcItem> personItem = new BeanItem<HhcalcItem>(item); // item from
																	// POJO

		// Create the Form
		final Form itemForm = new ItemFormLayout(personItem);

		// Add form to layout
		addComponent(itemForm);

		// The cancel / apply buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button save = new Button("Save", new Button.ClickListener() {
			private static final long serialVersionUID = 1L;

			public void buttonClick(ClickEvent event) {
				try {
					itemForm.commit();
				} catch (Exception e) {
					// Ingnored, we'll let the Form handle the errors
				}
			}
		});
		buttons.addComponent(save);

		itemForm.getFooter().setMargin(true);
		itemForm.getFooter().addComponent(buttons);

	}
}